package com.mybatis.moudles.httpclient;

import com.google.gson.JsonObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.ResourceBundle;

public class MycookiesForGet {

    private String url;
    private ResourceBundle bundle;
    //用来存储cookies信息的变量
    private CookieStore store;

    @BeforeTest
    public void beforeTest(){
        bundle = ResourceBundle.getBundle("application");
        url = bundle.getString("test.url");
    }
    @Test
    public void testGetCookies() throws IOException {
        String result;
        String uri = bundle.getString("getCookies.uri");
        String testurl =this.url+uri;
        HttpGet get =new HttpGet(testurl);
        DefaultHttpClient client =new DefaultHttpClient();
        HttpResponse response =client.execute(get);
        result = EntityUtils.toString(response.getEntity());
        System.out.println(result);
        //获取cookies信息
        this.store =client.getCookieStore();
      /*  List<Cookie> cookieList = store.getCookies();
        for(int i=0;i<cookieList.size();i++){
            String name = cookieList.get(i).getName();
            String value = cookieList.get(i).getValue();
             System.out.println("name:"+name);
             System.out.println("value:"+value);
        }*/

    }
    @Test(dependsOnMethods = {"testGetCookies"})
    public void testGetWithCookies() throws IOException {
        String uri = bundle.getString("test.get.with.cookies");
        String testurl =this.url+uri;
        HttpGet get =new HttpGet(testurl);
        DefaultHttpClient client =new DefaultHttpClient();
        //设置cookies信息
        client.setCookieStore(this.store);
        HttpResponse response = client.execute(get);
        //获取响应的状态码
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        if(statusCode ==200){
           String result = EntityUtils.toString(response.getEntity());
           System.out.println(result);
        }
    }

    @Test(dependsOnMethods = {"testGetCookies"})
    public void testPostMethod() throws IOException {
        String uri =bundle.getString("test.post.with.cookies");
        //拼接最终的测试地址
        String testuri = this.url +uri;
        //声明一个client对象，用来进行方法的执行
        DefaultHttpClient client = new DefaultHttpClient();
        //声明一个方法，这个方法就是post方法
        HttpPost post = new HttpPost(testuri);
        //添加参数
        JSONObject param = new JSONObject();
        param.put("name","sdsd");
        param.put("age","343");
        //设置请求头信息,设置header信息
        post.setHeader("Content-type","application/json");
        //将参数信息添加到方法中
        StringEntity  entity =new StringEntity(param.toString(),"utf-8");
        post.setEntity(entity);
        //声明一个对象来进行响应结果的存储
        String result;
        //设置cookies信息
        client.setCookieStore(this.store);
        //执行post方法
        HttpResponse response = client.execute(post);
        //获取响应结果
         result = EntityUtils.toString(response.getEntity());
         System.out.println(result);
        //处理结果，就是判断返回结果是否符合预期
        JSONObject resultJson = new JSONObject(result);
        String success = (String) resultJson.get("huahansan");
        Assert.assertEquals("success",success);

    }


}
